﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TravelingSalesmanEvolution
{
    class road
    {
        int points;
        public int profit;
        public int length;
        int max;
        public bool valid;
        public bool tooLong;
        public int[] poi;
        List<int> dist;

        public int getPoints()
        {
            return points;
        }

        public int getMax()
        {
            return max;
        }

        public road(int p, int m)
        {
            points = p;
            length = 0;
            max = m;
            valid = true;
            tooLong = false;
            poi = new int[points];
            dist = new List<int>();
        }



        public void map(graph g)
        {
            int posi = 0, last = 0;

            //dist.AddRange(poi.Distinct());

            if (poi[0] != 0 || poi[poi.Count() - 1] != 0)
            {
                valid = false;
            }
            else
            foreach (int mi in poi)
            {
                if (posi == 0)
                {
                    last = mi;
                    posi++;
                    continue;
                }

                //bool error = true;
                int mark = 0;
                foreach (var t in g.poi[last].Item3)
                {
                    if (t.Item1 == mi + 1)
                    {
                        mark = t.Item2;
                        //error = false;
                        break;
                    }
                    mark++;
                }

                //if (error) System.Console.WriteLine("Błąd z " + last + " do " + mi + "\n");

                length += mark;


                if (!dist.Exists(f => f == mi))
                {
                    dist.Add(mi);
                    profit += g.poi[mi].Item2;
                }
                last = mi;
                posi++;
            }

            if (length > max)
                tooLong = true;
        }


        public bool canDelete(int no, graph g)
        {
            bool bol = false;
            foreach (var t in g.poi[poi[no - 1]].Item3)
            {
                if (t.Item1 == poi[no + 1] + 1)
                {
                    bol = true;
                    break;
                }
            }
            return bol;
        }

        public bool cityExists(int no)
        {
            foreach (int en in poi)
            {
                if (en == no) return true;
            }
            return false;
        }

        public String show()
        {

            String tmp = points + " punktów, " + profit + " profitu, " + length + " długości, ";
            if (!valid)
                tmp += "nieprawidłowa, ";
            if (tooLong)
                tmp += "za długa, ";

            tmp += "punkty:\n";

            foreach (int e in poi)
            {
                tmp += (e + 1) + " ";
            }

            tmp += "\n";

            return tmp;
        }

        public String show2()
        {
            String tmp = profit + "\n";

            return tmp;
        }





    }
}
